package com.wfuhui.modules.order.service.impl;

import java.math.BigDecimal;
import java.time.temporal.TemporalUnit;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wfuhui.common.utils.NumberUtil;
import com.wfuhui.common.utils.Query;
import com.wfuhui.modules.order.entity.FinanceDetail;
import com.wfuhui.modules.order.entity.FinanceGroup;
import com.wfuhui.modules.order.entity.FinanceUser;
import com.wfuhui.modules.order.entity.FinanceUserRefund;
import com.wfuhui.modules.goods.dao.GoodsDao;
import com.wfuhui.modules.goods.dao.GoodsPicDao;
import com.wfuhui.modules.goods.entity.GoodsEntity;
import com.wfuhui.modules.goods.entity.GoodsPicEntity;
import com.wfuhui.modules.member.dao.MemberDao;
import com.wfuhui.modules.member.entity.MemberEntity;
import com.wfuhui.modules.order.dao.OrderDao;
import com.wfuhui.modules.order.dao.OrderGoodsDao;
import com.wfuhui.modules.order.entity.OrderEntity;
import com.wfuhui.modules.order.entity.OrderGoods;
import com.wfuhui.modules.order.entity.OrderGoodsEntity;
import com.wfuhui.modules.order.service.OrderService;
import com.wfuhui.modules.store.dao.UserCouponDao;
import com.wfuhui.modules.store.entity.UserCouponEntity;



@Service("orderService")
public class OrderServiceImpl implements OrderService {
	@Autowired
	private OrderDao orderDao;
	@Autowired
	private OrderGoodsDao orderGoodsDao;
	@Autowired
	private GoodsDao goodsDao;
	@Autowired
	private GoodsPicDao goodsPicDao;
	@Autowired
	private UserCouponDao userCouponDao;
	@Autowired
	private MemberDao memberDao;
	
	@Override
	public OrderEntity queryObject(Integer orderId){
		OrderEntity order = orderDao.queryObject(orderId);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("orderId", order.getId());
		List<OrderGoodsEntity> orderGoodsList = orderGoodsDao.queryList(params);
		order.setOrderGoodsList(orderGoodsList);
		if(order.getCouponId() != null) {
			UserCouponEntity userCoupon = userCouponDao.queryObject(order.getCouponId());
			order.setUserCoupon(userCoupon);
		}
		return order;
	}
	
	@Override
	public List<OrderEntity> queryList(Map<String, Object> map){
		List<OrderEntity> orderList = orderDao.queryList(map);
		for (OrderEntity orderEntity : orderList) {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("orderId", orderEntity.getId());
			List<OrderGoodsEntity> orderGoodsList = orderGoodsDao.queryList(params);
			orderEntity.setOrderGoodsList(orderGoodsList);
		}
		return orderList;
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return orderDao.queryTotal(map);
	}
	
	@Override
	public void save(OrderEntity order){
		orderDao.save(order);
	}
	
	@Override
	public void update(OrderEntity order){
		orderDao.update(order);
	}
	
	@Override
	public void delete(Integer orderId){
		orderDao.delete(orderId);
	}
	
	@Override
	public void deleteBatch(Integer[] orderIds){
		orderDao.deleteBatch(orderIds);
	}

	@Override
	@Transactional
	public void createOrder(OrderEntity orderEntity) {
		orderEntity.setOrderNumber(NumberUtil.getOrderNumber());
		orderDao.save(orderEntity);
		List<OrderGoodsEntity> orderGoodsList = orderEntity.getOrderGoodsList();
		for (OrderGoodsEntity orderGoods : orderGoodsList) {
			GoodsEntity goods = goodsDao.queryObject(orderGoods.getGoodsId());
			orderGoods.setOrderId(orderEntity.getId());
			orderGoods.setGoodsName(goods.getGoodsName());
			orderGoods.setPicUrl(goods.getPicUrl());
			orderGoodsDao.save(orderGoods);
			goodsDao.reduceStock(orderGoods.getGoodsId(), orderGoods.getNum());
		}
	}

	@Override
	public void updateByOrderNumber(OrderEntity order) {
		orderDao.updateByOrderNumber(order);
	}

	@Override
	public List<FinanceGroup> queryGroup(Query query) {
		return orderDao.queryGroup(query);
	}

	@Override
	@Transactional
	public void payNotify(OrderEntity order) {
		OrderEntity o = new OrderEntity();
		o.setOrderNumber(order.getOrderNumber());
		o.setOrderStatus(2);
		o.setPayTime(new Date());
		orderDao.updateByOrderNumber(o);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("orderId", order.getId());
		List<OrderGoodsEntity> orderGoodsList = orderGoodsDao.queryList(params);
		for (OrderGoodsEntity orderGoods : orderGoodsList) {
			goodsDao.reduceStock(orderGoods.getGoodsId(), 1);
		}
		if(order.getCouponId() != null) {
			UserCouponEntity userCoupon = new UserCouponEntity();
			userCoupon.setId(order.getCouponId());
			userCoupon.setStatus(2);
			userCouponDao.update(userCoupon);
		}
		order.setPayTime(o.getPayTime());
	}

	@Override
	public OrderEntity queryByOrderNumber(String orderNumber) {
		return orderDao.queryByOrderNumber(orderNumber);
	}

	@Override
	public List<OrderGoods> queryStatistics(Map<String, Object> params) {
		return orderDao.queryStatistics(params);
	}

	@Override
	public BigDecimal queryTotalAmount(Map<String, Object> params) {
		return orderDao.queryTotalAmount(params);
	}

	@Override
	public List<FinanceUser> queryUser(Query query) {
		return orderDao.queryUser(query);
	}

	@Override
	public List<FinanceUserRefund> queryUserRefund(Query query) {
		return orderDao.queryUserRefund(query);
	}

	@Override
	public int queryTotalUserRefund(Map<String, Object> map) {
		return orderDao.queryTotalUserRefund(map);
	}

	@Override
	public List<FinanceDetail> queryFinanceList(Map<String, Object> params) {
		return orderDao.queryFinanceList(params);
	}

	@Override
	public List<Map<String, String>> queryOrderCount() {
		return orderDao.queryOrderCount();
	}

}
